package org.example.socket;

import org.example.xuleihuaiol.Student;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * 客户端
 */
public class MyClient {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // 创建Socket
        Socket socket = new Socket("127.0.0.1", 9090);
        //客户端：先创建输出流对象，后创建输入流
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        // 学生管理系统
        System.out.println("欢迎进入我的学生管理系统！");
        while (true) {
            System.out.println("请选择：1.查询 2.添加学生 0.退出");
            Scanner sc = new Scanner(System.in);
            int i = sc.nextInt();
            switch (i) {
                case 1: {
                    System.out.println("您选择的是查询模块");
                    Data data = new Data(1, "执行查询操作", null);
                    oos.writeObject(data);
                    oos.flush();
                    Data d =(Data) ois.readObject();
                    System.out.println("查询学生信息："+d.getObj());
                    break;
                }
                case 2: {
                    System.out.println("您选择的是添加学生");
                    System.out.println("请输入学生学号：");
                    Integer id = sc.nextInt();
                    System.out.println("请输入学生姓名：");
                    String name = sc.next();
                    System.out.println("请输入学生年龄：");
                    Integer age = sc.nextInt();
                    Student student = new Student(id, name, age);
                    // 将学生对象，传输到服务端（写出）
                    Data data = new Data();
                    data.setCode(2);
                    data.setMsg("执行添加学生操作");
                    data.setObj(student);
                    oos.writeObject(data);
                    oos.flush();
//                    oos.close();
                    Object o = ois.readObject();
                    System.out.println(o);
//                    ois.close();
                    break;
                }
                default: {
                    System.out.println("其它操作");
                }
            }
        }
    }
    // 192.168.137.213
}

